﻿'Clinciu Andrei

Option Strict On

Imports Gift.BL
Imports System.Configuration
Public Class frmOrders

#Region "Events"

    'Fill the items with all existing orders
    Private Sub FillItems()
        flvOrders.Items.Clear()
        flvOrders.Colums = {"Persoon", "Datum Aanmaak", "Betaald", "Betaaltype", "Totale Prijs"}
        Dim dict As Dictionary(Of Integer, Order) = Order.GetOrdersFromDb(frmMain.dbh)
        For Each theOrder As Order In dict.Values

            Dim flvi As New FilteredListViewItem({theOrder.PersonName, theOrder.CreationDate, CStr(theOrder.Paid), theOrder.PaymentTypeName, CStr(theOrder.TotalPrice)}, theOrder)
            'flvProducts.lstView.Items.Add(flvi)
            flvOrders.Items.Add(flvi)
        Next

        flvOrders.FillListItems()
    End Sub
    Private Sub ShowProducts()
        If flvOrders.lstView.SelectedItems.Count <> 0 Then
            Dim dialog As New frmOrdersShowProducts
            'yes, give the list
            dialog.Products = CType(flvOrders.SelectedObject, Order).Products

            dialog.ShowDialog()
        End If
    End Sub

    Private Sub frmOrders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillItems()
    End Sub
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        'Wizard to make a new one
        Dim dialog As New frmOrdersWizard
        Dim showOrder As String = ""
        dialog.ShowWizard(1)
        dialog.nudTotal.Enabled = False
        'initialize the wizard and do all the things in the form itself
        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'write the changes to the database
            Console.WriteLine(CStr(CType(dialog.cboList.SelectedItem, GiftList).Id) & dialog.Products.Count & CType(dialog.cboOwner.SelectedItem, Person).Id & CType(dialog.cboPaymentType.SelectedItem, PaymentType).Name)
            Order.AddOrderToDb(frmMain.dbh, CType(dialog.cboList.SelectedItem, GiftList).Id, dialog.Products, CType(dialog.cboOwner.SelectedItem, Person), CType(dialog.cboPaymentType.SelectedItem, PaymentType))
            showOrder = "Geschenklijkst id: " & CType(dialog.cboList.SelectedItem, GiftList).Id & " voor " & CType(dialog.cboOwner.SelectedItem, Person).Naam & " " & CType(dialog.cboOwner.SelectedItem, Person).Voornaam & " Producten:" & vbNewLine & vbNewLine
            For Each prod In dialog.Products
                If prod.TotalOrdered > 0 Then
                    showOrder += prod.Product & "besteld: " & prod.TotalOrdered & vbNewLine
                End If
            Next

            MessageBox.Show(showOrder, "Uw bestelling", MessageBoxButtons.OK, MessageBoxIcon.Information)

            FillItems()
        End If
    End Sub
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'Wizard to edit an existing one with the choise of the owner disabled!
        If flvOrders.lstView.SelectedItems.Count <> 0 Then
            If CType(flvOrders.SelectedObject, Order).Paid Then
                MessageBox.Show("Deze bestelling kan niet worden bewerkt omdat het al betaald is.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                'Edit Order
                Dim dialog As New frmOrdersWizard

                'initialize the wizard and do all the things in the form itself
                Dim obj As Order = CType(flvOrders.SelectedObject, Order)
                dialog.EditWizard(obj.Id, obj.PaymentTypeName)
                dialog.nudTotal.Enabled = False
                If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    'write the changes to the database
                    Order.EditOrder(frmMain.dbh, obj.Id, dialog.Products, CType(dialog.cboBetalingsTypeEdit.SelectedItem, PaymentType), dialog.chkPaid.Checked)
                    FillItems()
                End If
                FillItems()
            End If
        Else
            MessageBox.Show("Selecteer een item om te bewerken.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Delete an order that wasn't paid
        If flvOrders.lstView.SelectedItems.Count <> 0 Then
            If CType(flvOrders.SelectedObject, Order).Paid Then
                MessageBox.Show("Deze bestelling kan niet worden verwijderd omdat het al betaald is.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                'delete order if not paid
                Order.DeleteOrder(frmMain.dbh, CType(flvOrders.SelectedObject, Order))
                FillItems()
            End If
        Else
            MessageBox.Show("Selecteer een item om te verwijderen.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        'Show products
        ShowProducts()
    End Sub
    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        ExportToWord()
    End Sub
#End Region

    Private Sub ExportToWord()
        If flvOrders.lstView.SelectedItems.Count <> 0 Then
            'sfd.Filter = "Word (*.doc)|*.doc|Word (*.docx)|*.docx"
            sfd.InitialDirectory = Application.StartupPath
            sfd.Title = "Selecteer de excelfile die u wilt opslaan"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Order.ExportToWord(frmMain.dbh, sfd.FileName, CType(flvOrders.SelectedObject, Order), My.Application.Info.DirectoryPath) ' of Application.StartupPath
                MessageBox.Show("Het factuur werd geëxporteerd.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Else
            MessageBox.Show("Selecteer bestelling die u wenst te exporteren.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub


    Private Sub btnNietBetaald_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNietBetaald.Click
        ExportToExcel()
    End Sub

    Private Sub ExportToExcel()
        If sfd2.ShowDialog = Windows.Forms.DialogResult.OK Then
            Order.exportToExcel(sfd2.FileName, My.Application.Info.DirectoryPath, frmMain.dbh)
            MessageBox.Show("De bestellingen werden geëxporteerd", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class